import {
  findAccounts,
  createAccount,
  updateAccount,
  deleteAccount,
  createRecord,
  updateRecord,
  findRecordMany,
  getBalance,
  getLastMonthBalance,
  deleteRecord, exportExcel, findRecordStatistics, findPaymentMany, findRecord,
  assess,taxAssess,findProcureRecordMany,procureRecordAssess,fixedAssess,
  findInvoiceRecords, createInvoiceRecord,updateInvoiceRecord,deleteInvoiceRecord,findInvoiceSalesMany,findInvoiceProcureMany,invoiceAssess,invoiceReceiptAssess
  // findMainCostMany,findShippingFeeMany,findPublicFeeMany,findLobarFeeMany
} from "../services/finance";

export default {
  namespace: 'finance',
  state: {},
  effects: {
    * exportExcel({payload}, {call, put}) {
      const response = yield call(exportExcel, payload);

      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * getBalance({payload}, {call, put}) {
      const response = yield call(getBalance, payload);

      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * getLastMonthBalance({payload}, {call, put}) {
      const response = yield call(getLastMonthBalance, payload);

      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * findAccounts({payload}, {call, put}) {
      const response = yield call(findAccounts, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {accounts: result}});
      return response;
    },
    * createAccount({payload}, {call, put}) {
      const response = yield call(createAccount, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * deleteAccount({payload}, {call, put}) {
      const response = yield call(deleteAccount, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * updateAccount({payload}, {call, put}) {
      const response = yield call(updateAccount, payload);
      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * createRecord({payload}, {call, put}) {
      const response = yield call(createRecord, payload);

      if (response) {
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * findRecordMany({payload}, {call, put}) {
      const response = yield call(findRecordMany, payload);
      const {table} = payload;
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({type: 'onNotify', payload: {message, type: 'error'}});
          return;
        }
        const _payload = {};
        _payload[`${table}Records`] = result;
        yield put({
          type: 'onDataLoad',
          payload: _payload
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * findProcureRecordMany({payload}, {call, put}) {
      const response = yield call(findProcureRecordMany, payload);
      const {table} = payload;
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({type: 'onNotify', payload: {message, type: 'error'}});
          return;
        }
        const _payload = {};
        _payload[`${table}Records`] = result;
        yield put({
          type: 'onDataLoad',
          payload: _payload
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * fixedAssess({payload}, {call, put}) {
      const response = yield call(fixedAssess, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {fixedAssess: result}});
      return response;
    },
    * procureRecordAssess({payload}, {call, put}) {
      const response = yield call(procureRecordAssess, payload);

      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {statistics: result}
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * findRecordStatistics({payload}, {call, put}) {
      const response = yield call(findRecordStatistics, payload);

      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {statistics: result}
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }

      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * findRecord({payload}, {call, put}) {
      console.log('find recoredd payload', payload);
      const response = yield call(findRecord, payload);
      console.log('find recoredd', response);
      if (response) {
        const {result, status, message} = response;
        if (status !== "ok") {
          yield put({
            type: 'onNotify',
            payload: {message, type: 'error'}
          });
          return;
        }
        yield put({
          type: 'onDataLoad',
          payload: {record: result}
        });
        yield put({
          type: 'onNotify',
          payload: {message: "操作成功！", type: 'info'}
        });
        return response;
      }
      yield put({
        type: 'onDataLoad',
        payload: {record: {}}
      });
      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },


    * updateRecord({payload}, {call, put}) {
      const response = yield call(updateRecord, payload);
      if (response) {
        return response;
      }
      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },

    * deleteRecord({payload}, {call, put}) {
      const response = yield call(deleteRecord, payload);
      if (response) {
        return response;
      }
      yield put({
        type: 'onNotify',
        payload: {message: "无服务！", type: 'error'}
      });
    },
    * findPaymentMany({payload}, {call, put}) {
      console.log('ddd', payload);
      const response = yield call(findPaymentMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {mainCost: result}});
      return response;
    },


    * assess({payload}, {call, put}) {
      const response = yield call(assess, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {financeAssess: result}});
      return response;
    },

    * taxAssess({payload}, {call, put}) {
      const response = yield call(taxAssess, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {taxAssess: result}});
      return response;
    },
    * createInvoiceRecord({payload}, {call, put}) {
      const response = yield call(createInvoiceRecord, payload);
      console.log('rrrrr',response);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      const {type} = payload;
      yield put({type: 'onDataLoad', payload: {invoiceRecord: result}});
      return response;
    },
    * deleteInvoiceRecord({payload}, {call, put}) {
      const response = yield call(deleteInvoiceRecord, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      const {type} = payload;
      yield put({type: 'onDataLoad', payload: {invoiceRecord: result}});
      return response;
    },
    * updateInvoiceRecord({payload}, {call, put}) {
      const response = yield call(updateInvoiceRecord, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      const {type} = payload;
      yield put({type: 'onDataLoad', payload: {invoiceRecord: result}});
      return response;
    },
    * findInvoiceRecords({payload}, {call, put}) {
      const response = yield call(findInvoiceRecords, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      const {type} = payload;
      const _payload = {};
      _payload[`${type}Records`] = result;
      console.log('_payload',_payload)
      yield put({type: 'onDataLoad', payload: _payload});
      return response;
    },

    * findInvoiceSalesMany({payload}, {call, put}) {
      const response = yield call(findInvoiceSalesMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {salesInvoices: result}});
      return response;
    },
    * invoiceAssess({payload}, {call, put}) {
      const response = yield call(invoiceAssess, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {invoiceAssess: result}});
      return response;
    },

    * findInvoiceProcureMany({payload}, {call, put}) {
      const response = yield call(findInvoiceProcureMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {procureInvoices: result}});
      return response;
    },
    * invoiceReceiptAssess({payload}, {call, put}) {
      const response = yield call(invoiceReceiptAssess, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {invoiceReceiptAssess: result}});
      return response;
    },
    // * findShippingFeeMany({payload}, {call, put}) {
    //   const response = yield call(findShippingFeeMany, payload);
    //   if (!response) {
    //     yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
    //     return;
    //   }
    //   const {result, status, message} = response;
    //   if (status !== "ok") {
    //     yield put({type: 'onNotify', payload: {message, type: 'error'}});
    //     return;
    //   }
    //
    //   yield put({type: 'onDataLoad', payload: {mainCost: result}});
    //   return response;
    // },
    // * findPublicFeeMany({payload}, {call, put}) {
    //   const response = yield call(findPublicFeeMany, payload);
    //   if (!response) {
    //     yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
    //     return;
    //   }
    //   const {result, status, message} = response;
    //   if (status !== "ok") {
    //     yield put({type: 'onNotify', payload: {message, type: 'error'}});
    //     return;
    //   }
    //
    //   yield put({type: 'onDataLoad', payload: {mainCost: result}});
    //   return response;
    // },
    // * findLobarFeeMany({payload}, {call, put}) {
    //   const response = yield call(findLobarFeeMany, payload);
    //   if (!response) {
    //     yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
    //     return;
    //   }
    //   const {result, status, message} = response;
    //   if (status !== "ok") {
    //     yield put({type: 'onNotify', payload: {message, type: 'error'}});
    //     return;
    //   }
    //
    //   yield put({type: 'onDataLoad', payload: {mainCost: result}});
    //   return response;
    // },
  },

  reducers: {
    onDataLoad(state, {payload}) {
      state._notify = undefined;
      return {...state, ...payload};
    },
    onNotify(state, {payload}) {
      state._notify = payload;
      return {...state, ...payload};
    }
  }
};
